<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/12 0012
 * Time: 16:40
 */

namespace app\api\controller\v1\cleaner;


use app\api\controller\v1\Cleaner;
use app\currency\service\DateService;
use app\currency\service\OrderRoyalty;
use think\App;

class Wallet extends Cleaner
{
    /**
     * 获取钱包页面数据
     * 余额、积分赠送次数、今日收益、本月收益、自己做收益、我的销售收益、团队做收益、团队销售收益
     */
    public function index()
    {
        $result = [
            'balance' => $this->cleaner->balance + 0,
            'integral_present_num' => $this->cleaner->integral_present_num,
            'today' => OrderRoyalty::getRoyaltyPrice($this->cid, 'all','all',date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59')),
            'month' => OrderRoyalty::getRoyaltyPrice($this->cid, 'all','all',DateService::beginMonth(),DateService::endMonth()),
            'self_service' => OrderRoyalty::getRoyaltyPrice($this->cid,'service','self'),
            'self_sale' => OrderRoyalty::getRoyaltyPrice($this->cid,'bind','self'),
            'team_service' => OrderRoyalty::getRoyaltyPrice($this->cid,'service','team'),
            'team_sale' => OrderRoyalty::getRoyaltyPrice($this->cid,'bind','team'),
        ];
        $this->success('获取成功！',$result);
    }

    /**
     * 查询今日、本月、今年统计数据
     * @param $id
     * @throws \Exception
     */
    public function read($id)
    {
        if(!in_array($id,['today','month','year']))
        {
            throw new \Exception("method {$id} not found");
        }
        if($id == 'today')
        {
            $start_at = DateService::beginToday();
            $end_at = DateService::endToday();
        } elseif ($id == 'month') {
            $start_at = DateService::beginMonth();
            $end_at = DateService::endMonth();
        } elseif ($id == 'year')
        {
            $start_at = DateService::beginYear();
            $end_at = DateService::endYear();
        }
        $result = [
            'total' => OrderRoyalty::getRoyaltyPrice($this->cid,'all','all',$start_at,$end_at),
            'self_service' => OrderRoyalty::getRoyaltyPrice($this->cid,'service','self',$start_at,$end_at),
            'self_sale' => OrderRoyalty::getRoyaltyPrice($this->cid,'bind','self',$start_at,$end_at),
            'team_service' => OrderRoyalty::getRoyaltyPrice($this->cid,'service','team',$start_at,$end_at),
            'team_sale' => OrderRoyalty::getRoyaltyPrice($this->cid,'bind','team',$start_at,$end_at),
        ];
        $this->success('success',$result);
    }
}